package com.ql.gamelogm.entities;


import com.ql.gamelogm.repository.DataLogRepository;
import com.ql.gamelogm.service.DataLogService;
import com.ql.gamelogm.service.LogService;
import com.ql.gamelogm.service.MailService;
import com.ql.gamelogm.service.ManagerService;
import com.ql.gamelogm.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class RedisCache {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private DataLogRepository dataLogRepository;
    @Autowired
    private ManagerService managerService;
    @Autowired
    private LogService logService;
    @Autowired
    private DataLogService dataLogService;
    @Autowired
    private MailService mailService;

    /*2.OperationLog数据写入缓存*/
    public void setOplogRedis() {
        List<Log> logList = logService.findAllLogs();
        int count = logList.size();
        if ((redisUtils.hasKey("oplog"))) {
            int opLogListSize = (int) redisUtils.lGetListSize("oplog");
            if (count > opLogListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = opLogListSize; i <count ; i++) {
                    objectList.add(logList.get(i));
                }
                redisUtils.lSet("oplog", objectList);
            }
        }else{
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(logList.get(i));
            }
            redisUtils.lSet("oplog", objectList);
        }
    }
    /*获取OpLog缓存*/
    public List<Log> getLogRedis() {
        //重新获取，判断是否需要更新数据
        setOplogRedis();
        List<Log> logList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("oplog", 0, redisUtils.lGetListSize("oplog"));
        for (Object obj : objectList) {
            logList.add((Log)obj);
        }

        return logList;
    }








    /*3.玩家游戏数据写入Redis*/
    public void setDataLogRedis() {
        List<DataLog> dataLogList = dataLogService.findAllDataLogs();
        int count = dataLogList.size();
        if ((redisUtils.hasKey("data"))) {//有键值添加新增的
            int dataLogListSize = (int) redisUtils.lGetListSize("data");
            if (count > dataLogListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = dataLogListSize; i <count ; i++) {
                    objectList.add(dataLogList.get(i));
                }
                redisUtils.lSet("data", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(dataLogList.get(i));
            }
            redisUtils.lSet("data", objectList);
        }
    }
    /*获取DataLog缓存*/
    public List<DataLog> getDataLogRedis() {
        //重新获取，判断是否需要更新数据
        setDataLogRedis();
        List<DataLog> dataLogList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("data", 0, redisUtils.lGetListSize("data"));
        for (Object obj : objectList) {
            dataLogList.add((DataLog) obj);
        }
        /*重新加入ES*/
        dataLogRepository.deleteAll();
        dataLogRepository.saveAll(dataLogList);
        return dataLogList;

    }





    /*4.搜索记录数据写入Redis*/
    public void setSearchHistoryLogRedis() {
        List<SearchHistory> searchHistoryList = logService.findAllSearchHistory();
        int count = searchHistoryList.size();
        if ((redisUtils.hasKey("searchHistory"))) {//有键值添加新增的
            int searchHistoryListSize = (int) redisUtils.lGetListSize("searchHistory");
            if (count > searchHistoryListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = searchHistoryListSize; i <count ; i++) {
                    objectList.add(searchHistoryList.get(i));
                }
                redisUtils.lSet("searchHistory", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(searchHistoryList.get(i));
            }
            redisUtils.lSet("searchHistory", objectList);
        }
    }
    /*获取搜索记录缓存*/
    public List<SearchHistory> getSearchHistoryLogRedis() {
        //重新获取，判断是否需要更新数据
        setSearchHistoryLogRedis();
        List<SearchHistory> searchHistoryList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("searchHistory", 0, redisUtils.lGetListSize("searchHistory"));
        for (Object obj : objectList) {
            searchHistoryList.add((SearchHistory) obj);
        }
        return searchHistoryList;
    }







    /*5.搜索告警日志数据写入Redis*/
    public void setWarnLogRedis() {
        List<WarnLog> warnLogList = logService.findAllWarnLog();
        int count = warnLogList.size();
        if ((redisUtils.hasKey("warn"))) {//有键值添加新增的
            int WarnLogListSize = (int) redisUtils.lGetListSize("warn");
            if (count > WarnLogListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = WarnLogListSize; i <count ; i++) {
                    objectList.add(warnLogList.get(i));
                }
                redisUtils.lSet("warn", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(warnLogList.get(i));
            }
            redisUtils.lSet("warn", objectList);
        }
    }
    /*获取搜索记录缓存*/
    public List<WarnLog> getWarnLogRedis() {
        //重新获取，判断是否需要更新数据
        setWarnLogRedis();
        List<WarnLog> warnLogList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("warn", 0, redisUtils.lGetListSize("warn"));
        for (Object obj : objectList) {
            warnLogList.add((WarnLog) obj);
        }
        return warnLogList;
    }







    /*6.收件箱邮件写入Redis*/
    public void setMailInBoxRedis() {
        List<Mail> mailList = mailService.findAllMailsInBox();
        int count = mailList.size();
        if ((redisUtils.hasKey("mailinbox"))) {//有键值添加新增的
            int MailListSize = (int) redisUtils.lGetListSize("mailinbox");
            if (count > MailListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = MailListSize; i <count ; i++) {
                    objectList.add(mailList.get(i));
                }
                redisUtils.lSet("mailinbox", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(mailList.get(i));
            }
            redisUtils.lSet("mailinbox", objectList);
        }
    }
    /*收件箱邮件记录缓存*/
    public List<Mail> getMailInBoxRedis() {
        //重新获取，判断是否需要更新数据
        setMailInBoxRedis();
        List<Mail> mailList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("mailinbox", 0, redisUtils.lGetListSize("mailinbox"));
        for (Object obj : objectList) {
            mailList.add((Mail) obj);
        }
        return mailList;
    }
    /*更新MailInBox缓存*/
    public void updateMailState(int id) {
        //取得Redis全部数据
        List<Mail> mailList = (List) redisUtils.lGet("mailinbox", 0, redisUtils.lGetListSize("mailinbox"));
        /*判断数据中有无id的数据*/

       //取该数据所在下标
        for(int i=0;i<mailList.size();i++){
            if(mailList.get(i).getId()==id&&mailList.get(i).getState().equals("未读")){
                Mail mail=new Mail();
                mailList.get(i).setState("已读");//修改id为id的一条数据的state为已读
                mail=mailList.get(i);
                redisUtils.lUpdateIndex("mailinbox", i, mail);
            }
        }
    }
    /*更新MailInBox删除数据缓存并返回mail对象加入数据库中*/
    public Mail deleteMailInBox(int id) {
        //取得Redis全部数据
        List<Mail> mailList = (List) redisUtils.lGet("mailinbox", 0, redisUtils.lGetListSize("mailinbox"));
        /*判断数据中有无id的数据*/
        Mail mail=new Mail();
        int index=0;//取该数据所在下标
        for(int i=0;i<mailList.size();i++){
            if(mailList.get(i).getId()==id){

                //取得要删除的邮件对象
                mail=mailList.get(i);
                int size=(int)redisUtils.lGetListSize("maildeleted");
                index=i;
                //删除缓存中的邮件对象
                redisUtils.lRemove("mailinbox",1,mail);
                mail.setId(size+1);
                //保存到已删除邮件缓存中
                redisUtils.lSet("maildeleted",mail);
            }
        }
        return mail;
    }






    /*7.已发送邮件写入Redis*/
    public void setMailSendedRedis() {
        List<Mail> mailList = mailService.findAllMailsSended();
        int count = mailList.size();
        if ((redisUtils.hasKey("mailsended"))) {//有键值添加新增的
            int MailListSize = (int) redisUtils.lGetListSize("mailsended");
            if (count > MailListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = MailListSize; i <count ; i++) {
                    objectList.add(mailList.get(i));
                }
                redisUtils.lSet("mailsended", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(mailList.get(i));
            }
            redisUtils.lSet("mailsended", objectList);
        }
    }
    /*已发送邮件记录缓存*/
    public List<Mail> getMailSendedRedis() {
        //重新获取，判断是否需要更新数据
        setMailSendedRedis();
        List<Mail> mailList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("mailsended", 0, redisUtils.lGetListSize("mailsended"));
        for (Object obj : objectList) {
            mailList.add((Mail) obj);
        }
        return mailList;
    }
    /*更新MailInBox删除数据缓存并返回mail对象加入数据库中*/
    public Mail deleteMailSended(int id) {
        //取得Redis全部数据
        List<Mail> mailList = (List) redisUtils.lGet("mailsended", 0, redisUtils.lGetListSize("mailsended"));
        /*判断数据中有无id的数据*/
        Mail mail=new Mail();
        int index=0;//取该数据所在下标
        for(int i=0;i<mailList.size();i++){
            if(mailList.get(i).getId()==id){

                //取得要删除的邮件对象
                mail=mailList.get(i);
                int size=(int)redisUtils.lGetListSize("maildeleted");
                index=i;
                //删除缓存中的邮件对象
                redisUtils.lRemove("mailsended",1,mail);
                mail.setId(size+1);
                //保存到已删除邮件缓存中
                redisUtils.lSet("maildeleted",mail);
            }
        }
        return mail;
    }





    /*8.已删除邮件写入Redis*/
    public void setMailDeletedRedis() {
        List<Mail> mailList = mailService.findAllMailsDeleted();
        int count = mailList.size();
        if ((redisUtils.hasKey("maildeleted"))) {//有键值添加新增的
            int MailListSize = (int) redisUtils.lGetListSize("maildeleted");
            if (count > MailListSize) {
                List<Object> objectList = new ArrayList();
                for (int i = MailListSize; i <count ; i++) {
                    objectList.add(mailList.get(i));
                }
                redisUtils.lSet("maildeleted", objectList);
            }
        }else{//无键值全部添加
            List<Object> objectList = new ArrayList();
            for(int i=0;i<count;i++){
                objectList.add(mailList.get(i));
            }
            redisUtils.lSet("maildeleted", objectList);
        }
    }
    /*已删除邮件记录缓存*/
    public List<Mail> getMailDeletedRedis() {
        //重新获取，判断是否需要更新数据
        setMailDeletedRedis();
        List<Mail> mailList = new ArrayList();
        /*从Redis缓存中获取数据-----------------------*/
        List<Object> objectList = redisUtils.lGet("maildeleted", 0, redisUtils.lGetListSize("maildeleted"));
        for (Object obj : objectList) {
            mailList.add((Mail) obj);
        }
        return mailList;
    }













    /*Redis数据表长*/

    public void getSize() {
        System.out.println(redisUtils.lGetListSize("manager"));
    }



    /*删除缓存*/
    public void delete() {
        redisUtils.del("manager");
    }
}
